package com.yidu.ladder.base.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.ladder.base.domain.Route;
import com.yidu.ladder.base.domain.RouteVo;
import com.yidu.ladder.base.domain.Subset;
import com.yidu.ladder.base.service.RouteService;
import com.yidu.ladder.util.IDGenerator;

/**
 * 线路控制器
 * 
 * @author yy
 * @since 2019-12-2
 *
 */
@Controller
@RequestMapping("/base")
public class RouteController {

	/**
	 * 关联线路业务逻辑层
	 */
	@Autowired
	private RouteService routeService;

	public RouteService getRouteService() {
		return routeService;
	}

	public void setRouteService(RouteService routeService) {
		this.routeService = routeService;
	}

	/**
	 * 管理页面
	 * 
	 * @return 线路管理页的URL
	 */
	@RequestMapping(value = "route")
	public String route() {
		return "base/route/route";
	}

	/**
	 * 给下拉框赋值
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "subsets")
	public List<Subset> selectAllRoute() {
		// 执行查询下拉框的值
		return routeService.selectAllRoute();
	}

	/**
	 * 检索线路
	 * 
	 * @param roleVo 线路包装类
	 * @return 线路信息数据
	 */
	@ResponseBody
	@RequestMapping(value = "routes", method = RequestMethod.GET)
	public Map<String, Object> queryRoute(RouteVo routeVo) {
		// 执行分页查询路线方法
		Map<String, Object> routeMap = routeService.queryRouteByCriteriaWithPage(routeVo);
		// 返回结果
		return routeMap;
	}

	/**
	 * 添加线路
	 * 
	 * @param route 线路
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "route", method = RequestMethod.POST)
	public int addRoute(Route route) {
		route.setRouteId(IDGenerator.getId());
		route.setSubsetId(IDGenerator.getId());
		route.setUseable("Y");
		// 执行添加路线方法
		int insert = routeService.addRoute(route);
		// 返回添加结果
		return insert;
	}

	/**
	 * 修改线路
	 * 
	 * @param route 线路
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "route", method = RequestMethod.PUT)
	public boolean updateRoute(Route route) {
		// 执行修改路线方法
		boolean updateRoute = routeService.updateRoute(route);
		// 返回修改结果
		return updateRoute;
	}

	/**
	 * 冻结或还原线路
	 * 
	 * @param routeId 线路Id
	 * @param useable 是否可用
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "route", method = RequestMethod.DELETE)
	public boolean frozenOrRecoveRoute(String routeId, String useable) {
		// 执行冻结或还原线路方法
		boolean result = routeService.frozenOrRecoveRoute(routeId, useable);
		// 返回结果
		return result;

	}
}
